package com.hzdl.util;

import java.sql.SQLException;

/**
 * @author ：Reus CAI
 * @date ：Created in 2020/12/17 14:40
 * @description：事务管理工具类
 * @modified By：
 * @version: $
 */


public class TransationManager {

    private ConnectionUtil connectionUtil;

    public void setConnectionUtil(ConnectionUtil connectionUtil) {
        this.connectionUtil = connectionUtil;
    }


    public void startTransaction() {
        try {
            connectionUtil.getConnection().setAutoCommit(false);
            System.out.println(connectionUtil.getConnection());


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void commitTransaction() {
        try {
            connectionUtil.getConnection().commit();
            connectionUtil.removeConnection();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void rollback() {
        try {
            connectionUtil.getConnection().rollback();
            System.out.println(connectionUtil.getConnection());

            connectionUtil.removeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
